home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / Reference / DevCon / Milan_1991 / Devcon91.1 / Libraries / Intuition / other_examples / StrDemo / strhooks.c < prev   
Encoding:
C/C++ Source or Header  |  1992-09-01  |  3.7 KB  |  152 lines

  1. /* strhooks.c -- string gadget hooks    */
  2. /* WARNING: This file contains "callback" functions.
  3.  * You must disable stack checking for them to work:
  4.  *         "LC -v strhooks"
  5.  */
  6.  
  7. /*
  8. Copyright (c) 1989 Commodore-Amiga, Inc.
  9.  
  10. Executables based on this information may be used in software
  11. for Commodore Amiga computers. All other rights reserved.
  12. This information is provided "as is"; no warranties are made.
  13. All use is at your own risk, and no liability or responsibility
  14. is assumed.
  15. */
  16.  
  17. #include <exec/types.h>
  18. #include <devices/inputevent.h>
  19. #include <utility/hooks.h>
  20. #include <intuition/sghooks.h>
  21.  
  22.  
  23. #include "strgad.h"
  24.  
  25. struct Hook    tabhook;
  26. struct Hook    cyclehook;
  27.  
  28. /* forward in this file    */
  29. VOID    tabh();
  30. void    cycleh();
  31.  
  32. #define TABSCAN    (0x42)
  33.  
  34. #define SHIFTY (IEQUALIFIER_LSHIFT|IEQUALIFIER_RSHIFT)
  35.  
  36. #define D( x )    ;    /* debugging: on 'x', off ';'    */
  37.  
  38. void
  39. initMyHooks()
  40. {
  41.     void initHook();
  42.  
  43.     initHook( &tabhook, tabh );
  44.     initHook( &cyclehook, cycleh );
  45. }
  46.  
  47. /*
  48.  * This is a function which converts register-parameter
  49.  * hook calling convention into standard C conventions.
  50.  * It only works with Lattice C 5.0+
  51.  * 
  52.  * Without the fancy __asm stuff, you'd probably need to
  53.  * write this in assembler.
  54.  *
  55.  * You could conceivably declare all your C hook functions
  56.  * this way, and eliminate the middleman (you'd initialize
  57.  * the h_Entry field to your C function's address, and not
  58.  * bother with the h_SubEntry field).
  59.  *
  60.  * This is nice and easy, though, and since we're using the
  61.  * small data model, using a single interface routine like this
  62.  * (which does the necessary __saveds), it might
  63.  * actually turn out to be smaller to use a single entry point
  64.  * like this rather than declaring each of many hooks __saveds.
  65.  */
  66. ULONG __saveds __asm
  67. hookEntry( register __a0 struct Hook *hookptr,
  68.     register __a2 void    *object,
  69.     register __a1 void    *message )
  70. {
  71.     return ( (*hookptr->h_SubEntry)( hookptr, object, message ) );
  72. }
  73.  
  74. void
  75. initHook( hook, ccode )
  76. struct Hook    *hook;
  77. VOID        (*ccode)();
  78. {
  79.     hook->h_Entry = hookEntry;
  80.     hook->h_SubEntry = (ULONG (*)()) ccode;
  81.     hook->h_Data = 0;        /* I don't use it        */
  82. }
  83.  
  84.  
  85. void
  86. tabh( hook, sgw, msg )
  87. struct Hook    *hook;    /* which I don't use */
  88. struct SGWork    *sgw;
  89. ULONG        *msg;
  90. {
  91.     D( kprintf("tabh: sgw: %lx\n", sgw ) );
  92.  
  93.     if ( ( *msg == SGH_KEY ) && ( sgw->IEvent->ie_Code == TABSCAN ) )
  94.     {
  95.     sgw->Code = (sgw->IEvent->ie_Qualifier & SHIFTY)?
  96.         MYCODEBACKTAB: MYCODETAB;
  97.     sgw->Actions |= SGA_END;
  98.     sgw->Actions &= ~SGA_USE;
  99.     }
  100. }
  101.  
  102. UBYTE    *choices[] = {
  103.     (UBYTE *) "choice 1",
  104.     (UBYTE *) "choice 2",
  105.     (UBYTE *) "choice 3",
  106.     (UBYTE *) "choice 4",
  107.     (UBYTE *) "choice 5",
  108.     (UBYTE *) "choice 6",
  109. };
  110.  
  111. #define NUMCHOICES    ((sizeof (choices))/sizeof (UBYTE *))
  112. #define UPARROW        (0x4c)
  113. #define DOWNARROW    (0x4d)
  114.  
  115. void
  116. cycleh( hook, sgw, msg )
  117. struct Hook    *hook;    /* which I don't use */
  118. struct SGWork    *sgw;
  119. ULONG        *msg;
  120. {
  121.     static int    choice = 0;
  122.  
  123.     D( kprintf("cycleh: %lx, sgw: %lx\n", sgw ) );
  124.  
  125.     if ( *msg != SGH_KEY ) return;
  126.  
  127.     switch ( sgw->IEvent->ie_Code )
  128.     {
  129.     case TABSCAN:
  130.     sgw->Code = (sgw->IEvent->ie_Qualifier & SHIFTY)?
  131.         MYCODEBACKTAB: MYCODETAB;
  132.     sgw->Actions |= SGA_END;
  133.     sgw->Actions &= ~SGA_USE;
  134.     break;
  135.  
  136.     case UPARROW:
  137.     D( kprintf("cycle hook (up), previous choice %ld\n", choice ));
  138.     sgw->WorkBuffer = choices[ choice = (choice + 1)%NUMCHOICES ];
  139.     sgw->NumChars = strlen( sgw->WorkBuffer );
  140.     D( kprintf("new choice: %ld <%s>\n",choice, sgw->WorkBuffer ) );
  141.     break;
  142.  
  143.     case DOWNARROW:
  144.     D( kprintf("cycle hook (down), previous choice %ld\n", choice ));
  145.     sgw->WorkBuffer = choices[choice=(choice+(NUMCHOICES-1))%NUMCHOICES];
  146.     sgw->NumChars = strlen( sgw->WorkBuffer );
  147.     D( kprintf("new choice: %ld <%s>\n",choice, sgw->WorkBuffer ) );
  148.     break;
  149.     }
  150. }
  151.  
  152.